package com.admin.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.util.Respons.TestSocketBean;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.LinkedBlockingQueue;

//定时器
@EnableScheduling
@Component
public class SchedulingController {
    /*  @Autowired
      private StringRedisTemplate stringRedisTemplate;*/
    //队列发送
    @Autowired
    private AmqpTemplate rabbitTemplate;
    //订阅发送的 交换器
    @Autowired
    private FanoutExchange fanoutExchange;

    //向redis消息队列 通道 发布消息  并通过websocket分散在各个客户端
    @Scheduled(fixedRate = 5000)
    public void sendMessage() {
        LinkedBlockingQueue link1 = new LinkedBlockingQueue();
        LinkedBlockingQueue link2 = new LinkedBlockingQueue();
        Random random = new Random();

        for (int i = 0; i < 10; i++) {
            // link1.add("{\"type\":\"" + random.nextInt(4) + "\",\"moeny\":\"" + random.nextInt(10000) + "\"}");
        }
        for (int i = 0; i < 10; i++) {
            // link2.add("{\"type\":\"" + random.nextInt(4) + "\",\"moeny\":\"" + random.nextInt(10000) + "\"}");
        }
        Map map = new HashMap();
        map.put("link1", link1);
        map.put("link2", link2);
        TestSocketBean testSocketBean = new TestSocketBean();
        testSocketBean.setData(JSON.toJSONString(map));
        testSocketBean.setType(2);
        //   stringRedisTemplate.convertAndSend("data", JSON.toJSONString(map));
        //rabbitTemplate.convertAndSend(fanoutExchange.getName(), " ", testSocketBean);
        int flag = 0;
        for (int i = 0; i < link1.size(); i++) {
            try {
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //把队列一的数据转map
            Map map1 = JSONObject.parseObject(link1.poll().toString(), Map.class);
            for (int j = 0; j < link2.size(); j++) {
                Map map2 = JSONObject.parseObject(link2.poll().toString(), Map.class);
                //两个队列的某一个type相同  计算价格
                if (map1.get("type").equals(map2.get("type"))) {
                    //标识有没有交易成功过
                    flag += 1;
                    int moeny = Integer.parseInt(map1.get("moeny") + "") - Integer.parseInt(map2.get("moeny") + "");
                    // stringRedisTemplate.convertAndSend("moeny",  moeny+"");
                    testSocketBean = new TestSocketBean();
                    testSocketBean.setType(1);
                    testSocketBean.setMoeny(moeny + "");
                    //rabbitTemplate.convertAndSend(fanoutExchange.getName(), "pic.moeny.Listener", testSocketBean);
                }
            }

        }
        testSocketBean = new TestSocketBean();

        if (flag == 0) {
            testSocketBean.setType(3);
            testSocketBean.setEnd("<a style='color:red'>本次交易完毕,本次模拟无交易</a>");
            //  stringRedisTemplate.convertAndSend("end", "<a style='color:red'>本次交易完毕,本次模拟无交易</a>");
            //  rabbitTemplate.convertAndSend(fanoutExchange.getName(),"pic.end.Listener", testSocketBean);
        } else {
            testSocketBean.setType(3);
            testSocketBean.setEnd("<a style='color:red'>本次交易完毕</a>");
            //  stringRedisTemplate.convertAndSend("end", "<a style='color:red'>本次交易完毕。</a>");
            //   rabbitTemplate.convertAndSend(fanoutExchange.getName(), "pic.end.Listener", testSocketBean);
        }

    }
}